<?php
$sMsg="";
$Action="?mod=news.listall.add&method=add";
$Submit="Đồng ý";
$TitleTable="Tạo mới";
if ($Method=="add")
{
	$Action = "?mod=news.listall.add&method=add";
	$TitleTable = "Thêm tin tức";
	
	
	if(isset($_POST['submit'])){
		$tt_tieude = addslashes($_POST['tt_tieude']);
		$tt_loaitintuc = $_POST['tt_loaitintuc'];
		$tt_tomtat = addslashes($_POST['tt_tomtat']);

		$tt_hinhanh = $upload->upload_file($PathFile,'tt_hinhanh');
		if($tt_hinhanh != ""){
			$filetype = $upload->file_type($tt_hinhanh);
			$upload->crop_image($imgWidth,$imgHeight,$PathFile . $tt_hinhanh,$filetype,$PathThumbFile .$tt_hinhanh);
		}
		
		$tt_noidung = addslashes($_POST['tt_noidung']);
		$tt_nguoidang = addslashes($_POST['tt_nguoidang']);
		
		$db->query("INSERT INTO ".$prefix."tintuc(tt_loaitintuc,tt_tieude,tt_hinhanh,tt_tomtat,tt_noidung,tt_ngaythang,tt_nguoidang,tt_hienthi) VALUES('".$tt_loaitintuc."','".$tt_tieude."','".$tt_hinhanh."','".$tt_tomtat."','".$tt_noidung."',now(),'".$tt_nguoidang."',1)");
		header("Location: ?mod=news.listall.add&method=add");
	}
	
}
if($Method=="edit")
{
	if(isset($_GET['id'])){
		$id = intval($_GET['id']);
	}
	
	if(isset($_POST['submit'])){
		$tt_tieude = addslashes($_POST['tt_tieude']);
		$tt_loaitintuc = $_POST['tt_loaitintuc'];
		$tt_tomtat = addslashes($_POST['tt_tomtat']);
		
		
		$tt_hinhanh_tmp = $upload->upload_file($PathFile,'tt_hinhanh');
		if($tt_hinhanh_tmp != ''){
			$filetype = $upload->file_type($tt_hinhanh_tmp);
			$upload->crop_image($imgWidth,$imgHeight,$PathFile . $tt_hinhanh_tmp,$filetype,$PathThumbFile .$tt_hinhanh_tmp);
			$tt_hinhanh = "'" . $tt_hinhanh_tmp . "'";
		}else{
			$tt_hinhanh = "`tt_hinhanh`";
		}
		
		if($tt_hinhanh_tmp == ""){
			$tt_hinhanh = '`tt_hinhanh`';
		}else{
			$tt_hinhanh = "'" . $tt_hinhanh_tmp . "'";
		}
		
		$tt_noidung = addslashes($_POST['tt_noidung']);
		$tt_nguoidang = addslashes($_POST['tt_nguoidang']);
		
		
		$db->query("UPDATE ".$prefix."tintuc SET tt_loaitintuc = '".$tt_loaitintuc."',tt_tieude = '".$tt_tieude."',tt_tomtat = '".$tt_tomtat."',tt_hinhanh = ".$tt_hinhanh.",tt_noidung = '".$tt_noidung."',tt_nguoidang = '".$tt_nguoidang."' WHERE tt_id = '".$id."'");
		header("Location: ?mod=news.listall");
	}
	
	
	
	$sql="select * from ".$prefix."tintuc where tt_id = '".$id."'";
	$result=mysql_query($sql);
	if(mysql_num_rows($result))
	{
		$row=mysql_fetch_array($result);
		$tt_id = $row[tt_id];
		$tt_loaitintuc = $row[tt_loaitintuc];
		$tt_tieude = $myString->unQuote(stripslashes($row[tt_tieude]));
		$tt_tomtat = $myString->unQuote(stripslashes($row[tt_tomtat]));
		$tt_noidung = $myString->unQuote(stripslashes($row[tt_noidung]));
		$tt_hinhanh= $row[tt_hinhanh];
		$tt_nguoidang = $row[tt_nguoidang];
		$tt_noidung = $row[tt_noidung];
		
	}
	
	$Action = "?mod=news.listall.add&method=edit&id=".$id."";
	$TitleTable = "Sửa tin tức mã : ".$id." ";
}

?>


<!--Bat dau form tao sua -->
<form action="<?=$Action;?>" method="post" enctype="multipart/form-data" name="frmHuyen" id="frmHuyen" onSubmit="return CheckFrm(this);">
	<table width="100%" border="1" style="border-collapse:collapse " bordercolor="#B2DAEE">
	  <tr class="newsList-content-head">
		<td height="28" colspan="2" align="center"><?=$TitleTable?></td>
	  </tr>		  
	  <tr>
	      <td width="254" align="right">Tiêu đề </td>
	      <td width="497">
	      <input name="tt_tieude" type="text" class="TextBox" id="tt_tieude" value="<?=$tt_tieude?>" size="80"></td>
	  </tr>
	  <tr>
		<td width="254" align="right">Chủng loại : </td>
		<td width="497">
		<select name="tt_loaitintuc" id="tt_loaitintuc">
			<?
				$rs = $db->query("SELECT * FROM ".$prefix."loaitintuc WHERE ltt_hienthi = 1 ORDER BY ltt_thutu ASC");
				while($row = $db->fetch_array($rs)){
			?>
			<option value="<?=$row[ltt_id]?>" <? if($tt_loaitintuc == $row[ltt_id]){ ?> selected="selected" <? } ?>><?=$row[ltt_ten]?></option>
			<? } ?>
		  </select>		</td>
	  </tr>
	  <tr>
		<td width="254" align="right">Tóm tắt : </td>
		<td width="497"><textarea name="tt_tomtat" cols="77" rows="6" id="tt_tomtat"><?=$tt_tomtat?></textarea></td>
	  </tr>	  
	  <tr>
		<td width="254" align="right">Hình ảnh : </td>
		<td width="497"><input name="tt_hinhanh" type="file" id="tt_hinhanh" size="80" />
		<? if($Method == "edit"){ ?>
			<br />
			<img src="../upload/<?=$tt_hinhanh?>" width="120" />
		<? } ?>
		</td>
	  </tr>
	  <tr>
		<td width="254" align="right">Chi tiết : </td>
		<td width="497">
				<div>
					<br style="line-height:5px">
					<?php
					$oFCKeditor = new FCKeditor('tt_noidung');
					$oFCKeditor->BasePath = FCK_DIR;
					$oFCKeditor->Value = $tt_noidung;
					$oFCKeditor->Height = 400;
					$oFCKeditor->Width = '99%';
					$oFCKeditor->Create() ;
					?>
				</div>
		</td>
	  </tr>
	  <tr>
	    <td width="254" align="right">Người đăng : </td>
	    <td><input name="tt_nguoidang" type="text" id="tt_nguoidang" value="<?=$tt_nguoidang?>" size="80" /></td>
      </tr>
	  <tr>
		<td colspan="2" align="center">
		<input type="submit" name="submit" value="<?=$Submit?>" class="Button" onClick="return CheckFrmTl();">&nbsp;&nbsp;&nbsp;
		<input type="reset" name="reset" value="Làm lại" class="Button">&nbsp;&nbsp;&nbsp;
		<input type="button" name="back" value="Quay về" class="Button" onclick="GoBack('?mod=tintuc')"></td>
	  </tr>
	</table>
</form>

<br /><Br />
	